package com.web.exiaodao.remote.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.remote.service.ApiUserService;
import com.web.webstart.base.util.XaUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.exiaodao.remote.service.ApishareRecordTableService;
import com.web.exiaodao.remote.vo.ShareRecordTableVo;
import com.web.exiaodao.business.entity.ShareRecordTable;

/**
 * @author zhanglin
 * @version V1.0
 * @Title: ApiShareRecordTableController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 分享表信息接口
 * @date 2015年3月23日 下午1:00:00
 */
@Api(value = "ShareRecord", description = "分享接口", position = 10)
@Controller
@RequestMapping("/api/shareRecord")
public class ApiShareRecordTableController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ApiShareRecordTableController.class);

    @Autowired
    private ApishareRecordTableService shareRecordTableService;

    @Autowired
    private ApiUserService userService;

    /**
     * @return
     * @Title: findShareRecordTableList
     * @Description: 查询所有分享表信息
     */
    @ApiOperation(value = "查询所有分享", notes = "查询所有分享信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findShareRecordList", method = RequestMethod.POST)
    public XaResult<Page<ShareRecordTableVo>> findShareRecordTableList(
            @ApiParam("申请人ID,字段名:userId") @RequestParam(value = "userId") Long userId,
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
    ) throws BusinessException {
        XaResult<Page<ShareRecordTableVo>> xr = new XaResult<>();
        if (XaUtil.isEmpty(userId)) {
            xr.error("用户id不能为空");
            return xr;
        }
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{'search_EQ_userId':'" + userId + "'}", "search_");
        return shareRecordTableService.findListEQUStatusByFilterPage(XaConstant.Status.valid, filterParams, pageable);
    }

    /**
     * @Title: save
     * @Description: 新增一条实体
     */
    @ApiOperation(value = "添加分享", notes = ",新增分享,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "saveShareRecord", method = RequestMethod.POST)
    public XaResult<ShareRecordTableVo> saveShareRecordTable(
            @ApiParam("用户,字段名:userId") @RequestParam(value = "userId") Long userId,
            @ApiParam("2机构;22住;3商城;4玩;42:作者;游学5;服务公共1;12:家政护理; 13:医疗服务|医疗康复; 14:护理培训|培训义工 15:义工中心 ）,字段名:share_Type") @RequestParam(value = "share_Type") Integer share_Type,
            @ApiParam("分享的对象id,字段名:share_ObjectId") @RequestParam(value = "share_ObjectId") Long share_ObjectId,
            @ApiParam("分享平台(1:微信 2：微博 3:微信朋友圈),字段名:share_platform") @RequestParam(value = "share_platform") Long share_platform,
            HttpServletRequest request
    ) throws BusinessException {
        XaResult<ShareRecordTableVo> xr = new XaResult<>();
        if (XaUtil.isEmpty(userId)) {
            xr.error("用户id不能为空");
            return xr;
        }
        try {
            //用户要存在
            userService.findOne(userId);

            if (XaUtil.isEmpty(share_platform)) {
                xr.error("share_platform不能为空");
                return xr;
            }
            if (XaUtil.isNotEmpty(share_platform) && !JConstant.SharePlatformType.isConstantVal(share_platform)) {
                xr.error("share_platform错误");
                return xr;
            }
            if (XaUtil.isEmpty(share_Type)) {
                xr.error("share_Type不能为空");
                return xr;
            }
            if (XaUtil.isNotEmpty(share_Type) && !JConstant.Search_Share_FavType.isConstantVal(share_Type)) {
                xr.error("share_Type错误");
                return xr;
            }
            if (share_ObjectId == null) {
                xr.error("share_ObjectId不能为空");
                return xr;
            }

            ShareRecordTable model = new ShareRecordTable();
            model.setUserId(userId);
            model.setShareType(share_Type);
            model.setShareObjectId(share_ObjectId);
            model.setSharePlatform(share_platform);
            return shareRecordTableService.createModel(model);
        } catch (Exception e) {
            xr.error(XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error, new Throwable(e));
        }
        return xr;
    }

}

